package com.my.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.my.demo.pojo.Notice;
import com.my.demo.pojo.Result;
import com.my.demo.pojo.User;
import com.my.demo.service.NoticeService;
import com.my.demo.service.UserService;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author liangyuanshao
 * @date 2021/12/21 - 19:44
 */
@RestController
@CrossOrigin
public class FeedBackNoticeController {@Autowired
NoticeService noticeService;
  @Autowired
  UserService userService;
  @PostMapping("/notice_someone")
  public Result noticeSomeone(@RequestBody Map<String, String> map){
    Result result=new Result();
    result.setSuccess(false);
    result.setDetail(null);
    Integer userId=Integer.parseInt(map.get("userId"));
    String content=map.get("content");
    Notice notice=new Notice(content,userId);
    noticeService.save(notice);
    result.setSuccess(true);
    result.setDetail(notice);
    result.setMsg("回复成功");
    return result;
  }

  @PostMapping("/get_notice")
  public Result getNotice(){
    Result result=new Result();
    result.setSuccess(false);
    result.setDetail(null);

    String userName = (String) SecurityUtils.getSubject().getPrincipal();
    Integer userId = userService.selectIdByName(userName);

    QueryWrapper<Notice> noticeQueryWrapper= new QueryWrapper<>();
    noticeQueryWrapper.eq("notice_receiver",userId).or().eq("notice_receiver",-1);
    noticeQueryWrapper.orderByAsc("notice_time");
    List<Notice> list = noticeService.list(noticeQueryWrapper);
    result.setSuccess(true);
    result.setDetail(list);
    result.setMsg("获取消息成功");
    return result;
  }

  @PostMapping("/set_notice_state")
  public Result setNoticeState(@RequestBody Map<String,Integer> map){
    Result result=new Result();
    result.setSuccess(false);
    result.setDetail(null);
    Integer noticeId=map.get("noticeId");
    UpdateWrapper<Notice> noticeUpdateWrapper =new UpdateWrapper<>();
    noticeUpdateWrapper.eq("notice_id",noticeId).set("notice_state",1);
    noticeService.update(noticeUpdateWrapper);
    result.setSuccess(true);
    result.setMsg("设置成功");
    return result;
  }




}
